package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.model.Entity.TourGroup;
import org.yty.travelagencymongodb.service.TourGroupService;

@RestController
@RequestMapping("tour-group")
@RequiredArgsConstructor
public class TourGroupController {
    private final TourGroupService tourGroupService;

    @GetMapping("/{id}")
    public Result<TourGroup> getTourGroupById(@PathVariable Long id) {
        TourGroup tourGroup = tourGroupService.getById(id);
        if (tourGroup != null) {
            return Result.ok(tourGroup);
        } else {
            return Result.error("旅游团不存在");
        }
    }

    @PostMapping
    public Result<Void> saveTourGroup(@RequestBody TourGroup tourGroup) {
        if (tourGroupService.save(tourGroup)) {
            return Result.ok();
        } else {
            return Result.error("保存失败");
        }
    }

    @PutMapping
    public Result<Void> updateTourGroup(@RequestBody TourGroup tourGroup) {
        if (tourGroupService.update(tourGroup)) {
            return Result.ok();
        } else {
            return Result.error("更新失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteTourGroupById(@PathVariable Long id) {
        tourGroupService.removeById(id);
        return Result.ok();
    }
}
